$(function () {

    //加载提现账户信息
    findUserCashAccount();
    //加载提现申请记录列表
    cashRecordList(1);
    //初始化时间控件
    initLayDate();

    $("body").on('click','.binding-account',function () {//绑定账户
        var _this = $(this);
        var accountType = _this.data('type');
        initBindingWin(accountType);
    }).on('click','.show-reject-text',function () {//显示未通过原因
        var _this = $(this);
        var rejectText = _this.next('div').html();
        icModale(1,'',rejectText,null,'500px',function (index,els) {
            layer.close(index);
        });
    }).on('click','#search-btn',function () {//搜索
        cashRecordList(1);
    });

    /**
     * 初始化绑定账户窗口
     * @param accountType 账户类型，1支付宝，2微信
     */
    function initBindingWin(accountType){
        var obj = getUserCashAccountByType(accountType);
        var title = '';
        var context = '<div class="mt20 ml20 mr20">';
        context +='<div class="form-horizontal">';

        context +='<div class="form-group">';
        context +='<label class="col-sm-3 control-label text-right unFw" style="padding-top: 0">真实姓名</label>';
        context +='<div class="col-sm-8">';
        if(obj!=null){
            context +='<input class="form-control" id="realName" value="'+obj.realName+'" type="text"/>';
        }else{
            context +='<input class="form-control" id="realName" value="" type="text"/>';
        }
        context +='<p class="mt5"><span class="fs12 c-ccc">请输入真实姓名</span></p>';
        context +='</div> </div>';
        if(accountType==1){
            title = '绑定支付宝账号';
            context +='<div class="form-group">';
            context +='<label class="col-sm-3 control-label text-right unFw" style="padding-top: 0">支付宝账号</label>';
            context +='<div class="col-sm-8">';
            if(obj!=null){
                context +='<input class="form-control" id="account" value="'+obj.account+'" type="text"/>';
            }else{
                context +='<input class="form-control" id="account" value="" type="text"/>';
            }
            context +='<p class="mt5"><span class="fs12 c-ccc">请输入支付宝账号</span></p>';
            context +='</div> </div>';
        }
        if(accountType==2){
            title = '绑定微信账号';
        }
        context +='</div></div>';

        icModale(2,'<strong>'+title+'</strong>',context,'确认','520px',function(index, ele){
            var realName = ele.find("#realName").val();
            if(accountType==1){//绑定支付宝账户
                var account = ele.find("#account").val();
                //保存绑定的支付宝账户信息
                saveZfbCashAccount(index,realName,account);
            }
            if(accountType==2){//绑定微信
                saveWeXinAccount(index,realName);
            }
        });
    }

    /**
     * 通过类型，查询绑定的账户
     * @param accountType 账户类型
     * @returns {*}
     */
    function getUserCashAccountByType(accountType) {
        var obj = null;
        $.ajax({
            url:baseloaction +'/web/uc/account/findUserCashAccountByType',
            type:'post',
            dataType:'json',
            async:false,
            data:{
                'accountType':accountType
            },
            success:function (result) {
                if(result.success==true){
                    obj = result.entity;
                }
            }
        });
        return obj;
    }

    /**
     * 保存支付宝账户
     */
    function saveZfbCashAccount(index,realName,account) {
        $.ajax({
            url:baseloaction +'/web/uc/account/saveZfbCashAccount',
            type:'post',
            dataType:'json',
            data:{
                'realName':realName,
                'account':account
            },
            success:function (result) {
                if(result.success==true){
                    layer.close(index);
                    findUserCashAccount();
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，保存绑定支付宝账户失败');
            }
        });
    }

    /**
     * 保存绑定微信提现账号
     * @param index 下标
     * @param realName 提现真实姓名
     */
    function saveWeXinAccount(index,realName) {
        $.ajax({
            url: baseloaction +'/web/uc/account/bindingWeiXin',
            type:'post',
            dataType:'json',
            data:{
                'realName':realName
            },
            success:function (result) {
                if(result.success==true){
                    layer.close(index);
                    var link = result.entity;
                    var verificationKey = result.verificationKey;
                    var interval = null;
                    var context = '<div class="text-center"><div id="qrcode"></div><div>扫码绑定提现微信账号</div></div>';
                    layer.open({
                        type: 1,
                        title:'<strong>扫码绑定提现微信账户</strong>',
                        content: context,
                        btn: [' 确 定 '],
                        area: '520px',
                        btnAlign: 'c',
                        shade: [0.3, '#000000'],
                        skin: 'lay_ppComment__wrap',
                        yes: function(index, ele){
                            layer.close(index);
                            window.location.href = baseloaction +'/web/uc/account/myCash';
                        },
                        success:function (layer) {
                            var em = layer.find('#qrcode');
                            try{
                                document.createElement('canvas').getContext('2d');
                                $(em).qrcode({render:"canvas",height:200, width:200,correctLevel:0,text:link});
                            }catch (e){
                                $(em).qrcoverificationOrderStatusde({render:"table",height:200, width:200,correctLevel:0,text:link});
                            }
                            interval = setInterval(function () {
                                $.ajax({
                                    url:baseloaction +'/web/uc/account/verifyWeChatBindingStatus',
                                    type:'post',
                                    dataType:'json',
                                    data:{
                                        'verificationKey':verificationKey
                                    },
                                    success:function (result) {
                                        if(result.success==true){
                                            window.clearInterval(interval);
                                            window.location.href = baseloaction +'/web/uc/account/myCash';
                                        }
                                    },
                                    error:function (error) {
                                        if(interval!=null){
                                            window.clearInterval(interval);
                                        }
                                    }
                                })
                            },2000);
                        }
                    });
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，绑定提现微信账户失败');
            }
        });
    }


    /**
     * 加载提现账户信息
     */
    function findUserCashAccount() {
        $.ajax({
            url:baseloaction +'/web/uc/account/findUserCashAccount',
            type:'post',
            dataType:'html',
            success:function (html) {
                $("#cash-account-list-box").html(html);
            }
        });
    }

    /**
     * 加载提现申请记录
     * @param pageNo
     */
    function cashRecordList(pageNo) {
        var params = {};
        var status = $("#status").val();
        var beginTime = $("#beginTime").val();
        var endTime = $("#endTime").val();
        params.status = status;
        params.beginTime = beginTime;
        params.endTime = endTime;
        params = JSON.stringify(params);
        getPageAjaxData({
            url: baseloaction + '/web/uc/cash/ajaxFindUserCashRecordList',
            contextId: 'cash-record-list-box',
            pageNo: pageNo,
            pageCall: cashRecordList,
            params: params,
            callback: function (_pageNo) {

            }
        });
    }


    /**
     * 初始化时间插件
     */
    function initLayDate() {
        laydate({
            elem: '#beginTime',
            format: 'YYYY-MM-DD hh:mm:ss',
            istime: true,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        });
        laydate({
            elem: '#endTime',
            format: 'YYYY-MM-DD hh:mm:ss',
            istime: true,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        });
    }
});